package com.amikomgamedev.labirinhantu.entity;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.entity.sprite.Sprite;

import com.amikomgamedev.labirinhantu.game.Game_Data;

public class Entity_Camera_Menu implements Game_Data
{
	private float	center_X		= 0;
	private float	center_Y		= 0;
	private float	center_X_Camera	= 0;
	private float	center_Y_Camera	= 0;

//	private int moveRight	= 0;
//	private int moveDown	= 1;
//	private int moveLeft	= 2;
//	private int moveUp		= 3;
//	private int move		= -1;

	private float SPEED_X	= 0;
	private float SPEED_Y	= 0;
	private float TIME	= 1.5f;
	private float time	= 0;
	
	private ZoomCamera	m_Camera;
	private Sprite		m_Bg;
	
	public Entity_Camera_Menu(
			ZoomCamera p_Camera)
	{
		m_Camera	= p_Camera;
	}
	
	public void initCamera(Sprite p_Bg)
	{
		float m_Ratio_Width		= RES_WIDTH / p_Bg.getWidth();
		float m_Ratio_Height	= RES_HEIGHT / p_Bg.getHeight();

		center_X		= p_Bg.getX() + p_Bg.getWidth() / 2;
		center_Y		= p_Bg.getY() + p_Bg.getHeight() / 2;
		center_X_Camera	= m_Camera.getCenterX();
		center_Y_Camera	= m_Camera.getCenterY();
		
		m_Camera.setZoomFactor(
				m_Ratio_Height < m_Ratio_Width 
				? m_Ratio_Height 
				: m_Ratio_Width);
		
		m_Bg	= p_Bg;
		

		SPEED_X = (center_X - center_X_Camera) / TIME;
		SPEED_Y = (center_Y - center_Y_Camera) / TIME;

//		Utils.TRACE("center_X = " + center_X);
//		
//		Utils.TRACE("SPEED_X = " + SPEED_X);
//		Utils.TRACE("SPEED_Y = " + SPEED_Y);
		
		time	= 0;

//		cekMove();
	}
	
	public void moveCamera(float p_Sec)
	{
	    time += p_Sec;
	    
//	    if (stop!=true)
	    if (time >= TIME)
	    {
//		stop = true;
		p_Sec	= TIME - time;
		time	= 0;
		
		m_Camera.setCenter(m_Camera.getCenterX() + SPEED_X * p_Sec,
    		    m_Camera.getCenterY() + SPEED_Y * p_Sec);
	    }
	    else
	    {
        	    m_Camera.setCenter(m_Camera.getCenterX() + SPEED_X * p_Sec,
        		    m_Camera.getCenterY() + SPEED_Y * p_Sec);
	    }
//		if (move == moveRight)
//			m_Camera.setCenter(m_Camera.getCenterX() + SPEED * p_Sec
//					, m_Camera.getCenterY());
//		if (move == moveDown)
//				m_Camera.setCenter(m_Camera.getCenterX()
//						, m_Camera.getCenterY() + SPEED * p_Sec);
//		if (move == moveLeft)
//				m_Camera.setCenter(m_Camera.getCenterX() - SPEED * p_Sec
//						, m_Camera.getCenterY());
//		if (move == moveUp)
//				m_Camera.setCenter(m_Camera.getCenterX()
//						, m_Camera.getCenterY() - SPEED * p_Sec);
	}
	
//	public void cekMove()
//	{		
//		if (center_X > center_X_Camera
//				&& center_Y == center_Y_Camera)
//		{
//			move	= moveRight;
//			SPEED	= RES_WIDTH / TIME;
//		}
//		
//		if (center_X == center_X_Camera
//				&& center_Y > center_Y_Camera)
//		{
//			move	= moveDown;
//			SPEED	= RES_HEIGHT / TIME;
//		}
//		
//		if (center_X < center_X_Camera
//				&& center_Y == center_Y_Camera)
//		{
//			move	= moveLeft;
//			SPEED	= RES_WIDTH / TIME;
//		}
//		
//		if (center_X == center_X_Camera
//				&& center_Y < center_Y_Camera)
//		{
//			move	= moveUp;
//			SPEED	= RES_HEIGHT / TIME;
//		}
		
//	}
	
	public void centerCamera()
	{
		m_Camera.setCenter(
				m_Bg.getX() + (m_Bg.getWidth() / 2)
				, m_Bg.getY() + (m_Bg.getHeight() / 2));
	}
	
	public boolean isStopCamera()
	{
	    if (SPEED_X > 10 && m_Camera.getCenterX() >= center_X)
		  return true;
	    else if (SPEED_X < -10 && m_Camera.getCenterX() <= center_X)
		  return true;
	    else if (SPEED_Y > 10 && m_Camera.getCenterY() >= center_Y)
		    return true;
	    else if (SPEED_Y < -10 && m_Camera.getCenterY() <= center_Y)
		    return true;
	    else
		return false;
	    
	    
//		if ((move == moveLeft)
//				&&(m_Camera.getCenterX() <= center_X))
//		{
//			return true;
//		}
//		else if ((move == moveRight)
//				&&(m_Camera.getCenterX() >= center_X))
//		{
//			return true;
//		}
//		else if ((move == moveDown)
//				&&(m_Camera.getCenterY() >= center_Y))
//		{
//			return true;
//		}
//		else if ((move == moveUp)
//				&&(m_Camera.getCenterY() <= center_Y))
//		{
//			return true;
//		}
//		else 
//		{
//			return false;
//		}
	}
}
